package com.magicborrow.fragment;

import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.magicborrow.R;
import com.magicborrow.activity.BorrowStuffDetailActivity;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.enums.PriceModeEnum;

public class StuffBorrowRulesFragment extends Fragment {

    private TextView tvDepositRule;
    private TextView tvDeposit;
    private TextView rentRule;
    private TextView tvRent;
    private TextView transferMode;
    private TextView tvTransferMode;
    private TextView tvAddress;
    // private MapView flMap;

    // 定位相关
    private LocationClient mLocClient;
    public MyLocationListenner myListener = new MyLocationListenner();
    private MyLocationConfiguration.LocationMode mCurrentMode;
    private BitmapDescriptor mCurrentMarker;
    private static final int accuracyCircleFillColor = 0xAAFFFF88;
    private static final int accuracyCircleStrokeColor = 0xAA00FF00;

    private MapView mMapView;
    private BaiduMap mBaiduMap;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.stuff_borrow_rules_fragment, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tvDepositRule = (TextView) view.findViewById(R.id.tv_deposit_rule);
        tvDeposit = (TextView) view.findViewById(R.id.tv_deposit);
        rentRule = (TextView) view.findViewById(R.id.rent_rule);
        tvRent = (TextView) view.findViewById(R.id.tv_rent);
        transferMode = (TextView) view.findViewById(R.id.transfer_mode);
        tvTransferMode = (TextView) view.findViewById(R.id.tv_transfer_mode);
        tvAddress = (TextView) view.findViewById(R.id.tv_address);
        mMapView = (MapView) view.findViewById(R.id.fl_map);
        StuffBean stuffBean = ((BorrowStuffDetailActivity) getActivity()).getStuff();
        tvDeposit.setText("￥" + stuffBean.getDeposit() + "元");
        setDepositRul(stuffBean.getDeposit());
        tvRent.setText("￥" + stuffBean.getPrice() + "/"+PriceModeEnum.getModeStr(stuffBean.getPriceMode()));
        if (stuffBean.getLendMode() == 1) {
            tvTransferMode.setText("快递");
        } else {
            tvTransferMode.setText("面交");
        }
        tvAddress.setText(stuffBean.getAddress());
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMyLocationEnabled(true);
        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
        // 修改为自定义marker
        mCurrentMarker = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_marker);
        mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
                mCurrentMode, true, mCurrentMarker));
      /*  mLocClient = new LocationClient(getContext());
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        //option.setScanSpan(1000);
        mLocClient.setLocOption(option);
        mLocClient.start();*/
        Log.i("lon===", "" + stuffBean.getLon());
        Log.i("lat===", "" + stuffBean.getLat());
        MyLocationData locData = new MyLocationData.Builder()

                .latitude(stuffBean.getLat())
                .longitude(stuffBean.getLon()).build();
        mBaiduMap.setMyLocationData(locData);
        LatLng ll = new LatLng(stuffBean.getLat(),
                stuffBean.getLon());
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(ll).zoom(18.0f);
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));

    }

    @Override
    public void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    /* 1 - 1000 垫付70%
           1000 - 3000 垫付60%
           3000 - 10000 垫付70%
           10000 - 20000 垫付30%
   大于20000 不垫付*/
    public void setDepositRul(int deposit) {
        int depositRul = 0;
        if (deposit >= 1 && deposit < 1000)
            depositRul = deposit * 7 / 10;
        else if (deposit >= 1000 && deposit < 3000)
            depositRul = deposit * 6 / 10;
        else if (deposit >= 3000 && deposit < 10000)
            depositRul = deposit * 7 / 10;
        else if (deposit >= 10000 && deposit < 20000)
            depositRul = deposit * 3 / 10000;
        else {
            depositRul = 0;
        }
        if (depositRul != 0) {
            tvDepositRule.setText("平台垫付￥" + depositRul);
        } else {
            tvDepositRule.setText("不垫付");
        }


    }


    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);


        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    @Override
    public void onDestroy() {
        // 退出时销毁定位
        if (mLocClient != null) {
            mLocClient.stop();
        }
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }
}
